package net.baikentools.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import net.baikentools.model.ClassEntity;
import net.baikentools.model.FieldEntity;
import net.baikentools.model.MethodEntity;
import net.baikentools.model.PropertyEntity;
import net.baikentools.model.ReverseEngineeringResult;
import net.baikentools.page.reverseengineering.ReverseEngineeringCSharp;

public class ConsoleReader {

	private String LINE_SEPARATOR = System.getProperty("line.separator");

	private String commandLine;

	public ConsoleReader(String commandLine) {
		this.commandLine = commandLine;
	}

	public String getOutput() {
		String returnValue = "";

		try {
			Runtime rt = Runtime.getRuntime();

			Process proc = rt.exec(commandLine);
			InputStream stderr = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = null;

			while ((line = br.readLine()) != null)
				returnValue += line;

			int exitVal = proc.waitFor();
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return returnValue;
	}

	public static void main(String[] args) throws IOException,
			InterruptedException, JAXBException {
		ConsoleReader consoleReader = new ConsoleReader(
				"staruml-baikentool-csharpreverse.exe \"I:/Documents/GIT/staruml-baikentools/sourcefiles/staruml-baikentools-frontend/staruml-baikentools-frontend-csharpreverse/bin/Debug/Mono.Cecil.dll\" \"http://localhost:8080/staruml-baiken-tools/faces/reverseEngineeringCSharp.jspx\"");
		String test = consoleReader.getOutput();

		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				test.getBytes());

		JAXBContext context = JAXBContext.newInstance(
				ReverseEngineeringResult.class, ClassEntity.class,
				MethodEntity.class, FieldEntity.class, PropertyEntity.class);

		Unmarshaller unmarshal = context.createUnmarshaller();

		Marshaller marshaller = context.createMarshaller();

		ReverseEngineeringResult data = new ReverseEngineeringResult();

		data.setClasses(new ArrayList<ClassEntity>());

		ClassEntity classEntity = new ClassEntity();
		classEntity.setName("sss");
		classEntity.setFields(new ArrayList<FieldEntity>());
		data.getClasses().add(classEntity);

		marshaller.marshal(data, System.out);

		ReverseEngineeringResult cSharp = (ReverseEngineeringResult) unmarshal
				.unmarshal(byteArrayInputStream);

		System.out.println(cSharp);
		// InputStream inputStream = consoleReader.getOutput();
		//
		// int bufferSize = 1024;
		// byte[] data = new byte[bufferSize];
		// while ((inputStream.read(data)) != -1) {
		// System.out.write(data);
		// }
		//
		// inputStream.close();
	}
}
